//
//  smOrderInfoController.m
//  shimaoHouse
//
//  Created by yst yst on 12-6-29.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smOrderInfoController.h"
#import "smOrderSubmitController.h"
#import "smOrderSearchController.h"
#import "smMboSmTbOrder.h"


@interface smOrderInfoController () <UITextFieldDelegate>
@property (nonatomic ,strong) UILabel* custNameLabel;
@property (nonatomic, strong) UITextField* custName;
@property (nonatomic ,strong) UILabel* custAddressLabel;
@property (nonatomic, strong) UITextField* custAddress;
@property (nonatomic ,strong) UILabel* totalMomeyLabel;
@property (nonatomic, strong) UITextField* totalMomey;
@property (nonatomic,strong)  UILabel*      earnestMoneyLabel;
@property (nonatomic,strong)  UITextField*  earnestMoney;
@property (nonatomic,strong)  UILabel*      houseNameLabel;
@property (nonatomic,strong)  UITextField*  houseName;
@property (nonatomic, strong) smMboSmTbOrder* order;
@property (nonatomic, strong) NSString* flag;








@end

@implementation smOrderInfoController
@synthesize custName = _custName;
@synthesize custNameLabel = _custNameLabel;
@synthesize custAddressLabel = _custAddressLabel;
@synthesize custAddress = _custAddress;
@synthesize totalMomeyLabel = _totalMomeyLabel;
@synthesize totalMomey  = _totalMomey;
@synthesize earnestMoneyLabel = _earnestMoneyLabel;
@synthesize earnestMoney = _earnestMoney;
@synthesize houseNameLabel = _houseNameLabel;
@synthesize houseName = _houseName;
@synthesize order = _order;
@synthesize flag = _flag;






- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    
    //点击其他地方键盘消失
    UIButton* backBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBt setFrame: self.view.bounds];
    [backBt setBackgroundColor:[UIColor clearColor]];
    //定义点击时的响应函数
	[backBt addTarget:self action:@selector(missKeyBoard) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBt];
    
    
    //后退
    UIButton* backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(5, 5, 118, 52);
    [backButton setImage:[UIImage imageNamed:@"back_button_up.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"back_button_down.png"] forState:UIControlStateHighlighted];
    backButton.backgroundColor=[UIColor clearColor];
    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: backButton];
  
    //更新时,显示订单编号
    if(self.flag==@"update")
    {
    UILabel* programnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(31, 80, self.view.bounds.size.width, 30)];
    [programnameLabel setBackgroundColor:[UIColor clearColor]];
     programnameLabel.text = [NSString stringWithFormat:@"订单编号：%@",[self.order id_]];
     programnameLabel.font = [UIFont systemFontOfSize:28];
     programnameLabel.textColor=[UIColor colorWithRed:126/255.0 green:126/255.0 blue:126/255.0 alpha:1.0];
    [self.view addSubview:programnameLabel];
    }
    

    UILabel* programnameLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(65, 151, self.view.bounds.size.width, 30)];
    [programnameLabel1 setBackgroundColor:[UIColor clearColor]];
    [programnameLabel1 setText:@"世贸房产锦绣皇城"];
     programnameLabel1.font = [UIFont systemFontOfSize:27];
     programnameLabel1.textColor=[UIColor whiteColor];
    [self.view addSubview:programnameLabel1];
    

    self.custNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 200, 100, 40) ];
    [self.custNameLabel setText:@"客户名称"];
    self.custNameLabel.font = [UIFont systemFontOfSize:22];
    self.custNameLabel.textColor=[UIColor colorWithRed:124/255.0 green:124/255.0 blue:124/255.0 alpha:1.0];
    [self.custNameLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.custNameLabel];
    
    
    
    
    self.custName = [[UITextField alloc] initWithFrame:CGRectMake(180, 200, 400, 50)];
    [self.custName setPlaceholder:@"请输入客户名称"];
    self.custName.font = [UIFont systemFontOfSize:22];
    if(self.flag == @"update")
    self.custName.text = [NSString stringWithFormat:@"%@",[self.order custName]];
    [self.custName setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.custName setBackgroundColor:[UIColor colorWithRed:215/255.0 green:226/255.0 blue:243/255.0 alpha:1.0]];
    [self.custName setDelegate:self];
    [self.view addSubview:self.custName];
    
    
    
    self.custAddressLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 300, 100, 40) ];
    [self.custAddressLabel setText:@"客户地址"];
    self.custAddressLabel.font = [UIFont systemFontOfSize:22];
    self.custAddressLabel.textColor=[UIColor colorWithRed:124/255.0 green:124/255.0 blue:124/255.0 alpha:1.0];
    [self.custAddressLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.custAddressLabel];
    
    
    
    
    self.custAddress = [[UITextField alloc] initWithFrame:CGRectMake(180, 300, 400, 50)];
    [self.custAddress setPlaceholder:@"请输入客户地址"];
    self.custAddress.font = [UIFont systemFontOfSize:22];
    if(self.flag == @"update")
        self.custAddress.text = [NSString stringWithFormat:@"%@",[self.order custAddress]];
    [self.custAddress setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.custAddress setBackgroundColor:[UIColor colorWithRed:215/255.0 green:226/255.0 blue:243/255.0 alpha:1.0]];
    [self.custAddress setDelegate:self];
    [self.view addSubview:self.custAddress];
    
    
    self.houseNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 400, 100, 40) ];
    [self.houseNameLabel setText:@"房源号"];
    self.houseNameLabel.font = [UIFont systemFontOfSize:22];
    self.houseNameLabel.textColor=[UIColor colorWithRed:124/255.0 green:124/255.0 blue:124/255.0 alpha:1.0];
    [self.houseNameLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.houseNameLabel];
    
    
    
    
    self.houseName = [[UITextField alloc] initWithFrame:CGRectMake(180, 400, 400, 50)];
    [self.houseName setPlaceholder:@"请输入房源号"];
    self.houseName.font = [UIFont systemFontOfSize:22];
    if(self.flag == @"update")
        self.houseName.text = [NSString stringWithFormat:@"%@",[self.order houseId]];
    [self.houseName setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.houseName setBackgroundColor:[UIColor colorWithRed:215/255.0 green:226/255.0 blue:243/255.0 alpha:1.0]];
    [self.houseName setDelegate:self];
    [self.view addSubview:self.houseName];
    
    
    self.earnestMoneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 500, 100, 40) ];
    [self.earnestMoneyLabel setText:@"定金金额"];
    self.earnestMoneyLabel.font = [UIFont systemFontOfSize:22];
    self.earnestMoneyLabel.textColor=[UIColor colorWithRed:124/255.0 green:124/255.0 blue:124/255.0 alpha:1.0];
    [self.earnestMoneyLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.earnestMoneyLabel];
    
    
    
    
    self.earnestMoney = [[UITextField alloc] initWithFrame:CGRectMake(180, 500, 400, 50)];
    [self.earnestMoney setPlaceholder:@"请输入定金金额"];
    self.earnestMoney.font = [UIFont systemFontOfSize:22];
    if(self.flag == @"update")
        self.earnestMoney.text = [NSString stringWithFormat:@"%@",[self.order earnestMoney]];
    [self.earnestMoney setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.earnestMoney setBackgroundColor:[UIColor colorWithRed:215/255.0 green:226/255.0 blue:243/255.0 alpha:1.0]];
    [self.earnestMoney setDelegate:self];
    [self.view addSubview:self.earnestMoney];
    
    
    self.totalMomeyLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 600, 100, 40) ];
    [self.totalMomeyLabel setText:@"总金额"];
    self.totalMomeyLabel.font = [UIFont systemFontOfSize:22];
    self.totalMomeyLabel.textColor=[UIColor colorWithRed:124/255.0 green:124/255.0 blue:124/255.0 alpha:1.0];
    [self.totalMomeyLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.totalMomeyLabel];
    
    
    
    
    self.totalMomey = [[UITextField alloc] initWithFrame:CGRectMake(180, 600, 400, 50)];
    [self.totalMomey setPlaceholder:@"请输入总金额"];
    self.totalMomey.font = [UIFont systemFontOfSize:22];
    if(self.flag == @"update")
        self.totalMomey.text = [NSString stringWithFormat:@"%@",[self.order totalMoney]];
    [self.totalMomey setBorderStyle:(UITextBorderStyleRoundedRect)];
    [self.totalMomey setBackgroundColor:[UIColor colorWithRed:215/255.0 green:226/255.0 blue:243/255.0 alpha:1.0]];
    [self.totalMomey setDelegate:self];
    [self.view addSubview:self.totalMomey];
    
    
    
    
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setFrame:CGRectMake(26, 787,282, 73)];
    [cancelButton setImage:[UIImage imageNamed:@"cancel_button_up.png"] forState:UIControlStateNormal];
    [cancelButton setImage:[UIImage imageNamed:@"cancel_button_down.png"] forState:UIControlStateHighlighted];
    [cancelButton addTarget:self action:@selector(buttonClickCancel:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelButton];
    
    
    UIButton* tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton setFrame:CGRectMake(464, 787,282, 73)];
    [tempButton setImage:[UIImage imageNamed:@"comfirm_button_up.png"] forState:UIControlStateNormal];
    [tempButton setImage:[UIImage imageNamed:@"comfirm_button_down.png"] forState:UIControlStateHighlighted];
    [tempButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tempButton];
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"order_info_background.png"]]];
    
    
}

-(void) setItems: (NSObject*) obj title:(NSString*) tt something: (int) ii
{
    self.title = tt;   
    
}



//取消按钮动作
-(void) buttonClickCancel:(UIButton*) bt {
    [self.navigationController popViewControllerAnimated:YES]; 
}

//确认按钮动作
-(void) buttonClick:(UIButton*) bt {
    if(self.custName.text.length == 0)
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入客户名称"
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];	
        return;
    }
    if(self.custAddress.text.length == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入客户地址"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];	
        return;
    }
    if(self.houseName.text.length == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入房源号"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];	
        return;
    }
    if(self.earnestMoney.text.length == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入定金金额"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];	
        return;
    }
    if(self.totalMomey.text.length == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入总金额"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];	
        return;
    }
    //取当前页面值到确认页面
    smMboSmTbOrder* orderTemp = [[smMboSmTbOrder alloc] init];
    [orderTemp setCustName:self.custName.text];
    [orderTemp setCustAddress:self.custAddress.text];
    [orderTemp setHouseId:[NSNumber numberWithInt:[self.houseName.text intValue] ]];
    [orderTemp setEarnestMoney:self.earnestMoney.text];
    [orderTemp setTotalMoney:self.totalMomey.text];
    //更新时会 自带id
    if(self.flag == @"update")
    [orderTemp setId_:[self.order id_]];
    
    
    smOrderSubmitController* orderSubmitController = [[smOrderSubmitController alloc] init];

    
    [orderSubmitController setitems:orderTemp fl:self.flag];
    
    [self.navigationController pushViewController:orderSubmitController animated:YES];    
}


//返回
-(void) backClick:(UIButton*) bt {     
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void) missKeyBoard {
    [self.custName resignFirstResponder]; 
    [self.custAddress resignFirstResponder]; 
    [self.houseName resignFirstResponder]; 
    [self.earnestMoney resignFirstResponder]; 
    [self.totalMomey resignFirstResponder]; 

}




-(void) setItemsToInfo: (smMboSmTbOrder *) obj fl:(NSString *) flag{
    
    self.order=obj;
    self.flag = flag;

}



@end
